package com.violet.service.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.violet.domain.po.Product;
import com.violet.domain.vo.app.UnProductVo;
import com.violet.mapper.ProductMapper;
import com.violet.service.app.AProductService;
import com.violet.utils.TransitionUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class AProductServiceImpl extends ServiceImpl<ProductMapper, Product>
        implements AProductService {

    private final TransitionUtils<?, Product, UnProductVo> unProductT;

    @Override
    public List<UnProductVo> getUnProducts(List<String> ids) {
        if (ids.isEmpty()) return List.of();
        // 查询所有未上架的商品（或者库存小于 5 的）
        List<Product> products = baseMapper.selectList(
            new LambdaQueryWrapper<Product>()
                .in(Product::getId, ids)
                .and(wrapper ->
                    wrapper
                    .eq(Product::getStatus, false)
                    .or()
                    .le(Product::getStock, 5)
                )
        );
        return products.stream().map(product -> unProductT.transition(product, UnProductVo.class)).toList();
    }

}
